﻿
#include "zyLayoutGrid.h"

DarkMoonUI::zyLayoutGrid::zyLayoutGrid()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyLayoutGrid_WndProc);
	m_obj->setDestroy(zyLayoutGrid_Destroy);
	SetMember(L"ColCount", 2);
}
DarkMoonUI::zyLayoutGrid::~zyLayoutGrid()
{

}

void WINAPI DarkMoonUI::zyLayoutGrid::zyLayoutGrid_Destroy(zyObject* obj)
{
	zyLayout::zyLayout_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyLayoutGrid::zyLayoutGrid_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled){
	if (uMsg == zyLYM_LAYOUT) {
		zyLayoutGrid* pGrid = (zyLayoutGrid*)obj->getPtrData(L"this");
        pGrid->zyLayoutGrid_Layout(obj);
	}
	else if (uMsg == zyLYM_GETMINSIZE){
		zyLayoutGrid* pGrid = (zyLayoutGrid*)obj->getPtrData(L"this");
		return pGrid->zyLayoutGrid_GetMinSize(obj);
	}
	return zyLayout::zyLayout_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}
int DarkMoonUI::zyLayoutGrid::zyLayoutGrid_GetMinSize(zyObject* obj) {
    // 尝试直接获取预定义的最小宽度和高度
    int minWidth = static_cast<int>(reinterpret_cast<LONG_PTR>(obj->getPtrData(L"minWidth")));
    int minHeight = static_cast<int>(reinterpret_cast<LONG_PTR>(obj->getPtrData(L"minHeight")));

    if (minWidth != -1 && minHeight != -1) {
        return MAKEWPARAM(minWidth, minHeight);
    }

    // 初始化最小宽度和高度为0
    minWidth = 0;
    minHeight = 0;

    // 获取布局项列表
    auto* pItems = reinterpret_cast<std::vector<zyLayoutItem>*>(obj->getPtrData(L"LayoutItems"));
    if (!pItems || pItems->empty()) {
        return MAKEWPARAM(minWidth, minHeight);
    }

    // 获取布局模式
    int colCount = static_cast<int>(reinterpret_cast<LONG_PTR>(obj->getPtrData(L"ColCount")));
    size_t itemCount = pItems->size();
    int rowCount = static_cast<int>(std::ceil(static_cast<double>(itemCount) / colCount));

    for (auto& item : *pItems) {
        if (item.pObject) {
            zyObject* pObj = item.pObject;

            // 如果当前项是另一个布局，则获取其最小尺寸
            if (static_cast<int>(reinterpret_cast<LONG_PTR>(pObj->getPtrData(L"Type"))) == zyLayoutType_Layout) {
                BOOL handled = false;
                int minSize = (int)(LONG_PTR)(pObj->callWndProc(0, zyLYM_GETMINSIZE, 0, 0, &handled));
                item.minWidth = LOWORD(minSize);
                item.minHeight = HIWORD(minSize);
            }

            // 计算当前项的总最小尺寸（包括边距）
            RECT rcMargin = *item.pMargin;
            int totalMinWidth = item.minWidth + rcMargin.left + rcMargin.right;
            int totalMinHeight = item.minHeight + rcMargin.top + rcMargin.bottom;

            // 更新整体最小宽度和高度
            if (minWidth < totalMinWidth) {
                minWidth = totalMinWidth;
            }
            if (minHeight < totalMinHeight) {
                minHeight = totalMinHeight;
            }
        }
    }

    // 根据列数和行数计算最终的最小尺寸
    minWidth *= colCount;
    minHeight *= rowCount;

    return MAKEWPARAM(minWidth, minHeight);
}
void DarkMoonUI::zyLayoutGrid::zyLayoutGrid_Layout(zyObject* obj) {
    // 获取 LayoutItems 并进行类型安全检查
    auto pItems = static_cast<std::vector<zyLayoutItem>*>(obj->getPtrData(L"LayoutItems"));
    if (!pItems || pItems->empty()) return;

    // 获取列数并确保其为正整数
    int ColCount = (int)(LONG_PTR)obj->getPtrData(L"ColCount");
	RECT* pRect = (RECT*)obj->getPtrData(L"pRect");
	RECT rc = *pRect;
	int pos = 0;
    if (ColCount <= 0) return;

    int count = (int)pItems->size();
    int RowCount = (count + ColCount - 1) / ColCount; // 计算行数
    int w = (rc.right - rc.left) / ColCount;
    int h = (rc.bottom - rc.top) / RowCount;

    for (int i = 0; i < count; i++) {
        zyLayoutItem& pItem = (*pItems)[i];
        if (!pItem.pRect || !pItem.pMargin || !pItem.pItemRect) continue;

        RECT rcChild = *pItem.pRect;
        RECT rcMargin = *pItem.pMargin;
        int colIndex = i % ColCount;
        int rowIndex = i / ColCount;
        int posLeft = rc.left + colIndex * w;
        int posTop = rc.top + rowIndex * h;

        SetRect(pItem.pItemRect, posLeft, posTop, posLeft + w, posTop + h);
        RECT rcDes = zyGetAlignRectI(*pItem.pItemRect, pItem.align, rcChild.right - rcChild.left, rcChild.bottom - rcChild.top, rcMargin.left, rcMargin.top, rcMargin.right, rcMargin.bottom);
        SetRect(pItem.pRect, rcDes.left, rcDes.top, rcDes.right, rcDes.bottom);

        if (pItem.pObject) {
            try {
                zyObject* pObject = pItem.pObject;
                int Type = (int)(LONG_PTR)pObject->getPtrData(L"Type");
                if (Type == zyLayoutType_Layout) {
                    RtlMoveMemory(pObject->getPtrData(L"pRect"), &rcDes, sizeof(RECT));
                    BOOL bHand = false;
                    pObject->callWndProc(0, zyLYM_LAYOUT, 0, 0, &bHand);
                } else {
                    HWND hWndItem = reinterpret_cast<HWND>(pObject->getPtrData(L"hWnd"));
                    MoveWindow(hWndItem, rcDes.left, rcDes.top, rcDes.right - rcDes.left, rcDes.bottom - rcDes.top, TRUE);
                }
            } catch (...) {
                // 处理异常，确保程序稳定
                std::cerr << "Error processing layout item at index " << i << std::endl;
            }
        }
		(*pItems)[i] = pItem;
    }
}
int DarkMoonUI::zyLayoutGrid::列数(int 参数_列数) {
	int ColCount = (int)GetMember(L"ColCount");
	if (参数_列数 > 0) {
		SetMember(L"ColCount", 参数_列数);
	}
	return ColCount;
}
int DarkMoonUI::zyLayoutGrid::取行数() {
	std::vector<zyLayoutItem>* pItems = (std::vector<zyLayoutItem>*)GetMember(L"LayoutItems");
	if (!pItems || pItems->empty()) return 0;
    int count = (int)pItems->size();
	int ColCount = (int)GetMember(L"ColCount");
	if (ColCount <= 0) {
		return 0;
	}
	int RowCount = count / ColCount + (count % ColCount != 0 ? 1 : 0);
	return RowCount;
}